package com.gongyinlian.personalprofilewebsiteserver.controller;

import com.gongyinlian.personalprofilewebsiteserver.common.enums.ResultCode;
import com.gongyinlian.personalprofilewebsiteserver.common.exception.BusinessException;
import com.gongyinlian.personalprofilewebsiteserver.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 异常测试控制器
 * 用于测试全局异常处理器是否正常工作
 * 
 * @author personal-profile-website
 * @date 2024-10-27
 */
@Tag(name = "异常测试接口", description = "测试全局异常处理功能")
@RestController
@RequestMapping("/test/exception")
@Validated
public class ExceptionTestController {
    
    /**
     * 测试业务异常
     */
    @Operation(summary = "测试业务异常", description = "抛出BusinessException测试全局异常处理")
    @GetMapping("/business")
    public Result<Void> testBusinessException() {
        throw new BusinessException(ResultCode.USER_NOT_EXIST);
    }
    
    /**
     * 测试自定义业务异常
     */
    @Operation(summary = "测试自定义业务异常", description = "抛出自定义消息的业务异常")
    @GetMapping("/business-custom")
    public Result<Void> testBusinessExceptionWithCustomMessage() {
        throw new BusinessException("这是一个自定义的业务异常消息");
    }
    
    /**
     * 测试参数验证异常
     */
    @Operation(summary = "测试参数验证异常", description = "测试@NotBlank参数验证")
    @GetMapping("/validate")
    public Result<String> testValidateException(
            @NotBlank(message = "参数name不能为空") @RequestParam String name) {
        return Result.success("参数验证通过: " + name);
    }
    
    /**
     * 测试空指针异常
     */
    @Operation(summary = "测试空指针异常", description = "故意抛出NullPointerException")
    @GetMapping("/null-pointer")
    public Result<Void> testNullPointerException() {
        String str = null;
        str.length(); // 触发空指针异常
        return Result.success();
    }
    
    /**
     * 测试数组越界异常
     */
    @Operation(summary = "测试数组越界异常", description = "故意抛出ArrayIndexOutOfBoundsException")
    @GetMapping("/array-index")
    public Result<Void> testArrayIndexOutOfBoundsException() {
        int[] array = new int[5];
        int value = array[10]; // 触发数组越界异常
        return Result.success();
    }
    
    /**
     * 测试非法参数异常
     */
    @Operation(summary = "测试非法参数异常", description = "抛出IllegalArgumentException")
    @GetMapping("/illegal-argument")
    public Result<Void> testIllegalArgumentException() {
        throw new IllegalArgumentException("参数值不合法");
    }
    
    /**
     * 测试成功响应
     */
    @Operation(summary = "测试成功响应", description = "返回成功响应，用于对比")
    @GetMapping("/success")
    public Result<String> testSuccess() {
        return Result.success("异常处理器工作正常");
    }
    
    /**
     * 测试不同状态码的业务异常
     */
    @Operation(summary = "测试不同状态码", description = "根据参数返回不同状态码的异常")
    @GetMapping("/code/{code}")
    public Result<String> testDifferentCode(@PathVariable Integer code) {
        switch (code) {
            case 401:
                throw new BusinessException(ResultCode.UNAUTHORIZED);
            case 403:
                throw new BusinessException(ResultCode.FORBIDDEN);
            case 404:
                throw new BusinessException(ResultCode.NOT_FOUND);
            case 500:
                throw new BusinessException(ResultCode.INTERNAL_SERVER_ERROR);
            default:
                return Result.success("没有匹配的错误码");
        }
    }
}

